package command

import (
	"strings"
	"time"

	"ssdevice/common/constant"
	"ssdevice/common/dto"
	"ssdevice/common/utils"
	"ssdevice/internal/device/models"
)

type ProductInsertReq struct {
	Name         string                 `json:"name"`
	DeviceType   string                 `json:"deviceType" vd:"len($)>0"`
	ProductModel string                 `json:"model" vd:"len($)>0"`
	DeptId       string                 `json:"deptId" comment:"分组id"`
	Photo        string                 `json:"photo" comment:"产品图片"`
	Description  string                 `json:"description" comment:"产品描述"`
	View         map[string]interface{} `json:"view" comment:"个性页"`
	dto.UserInfo `search:"-"`

	DevConfig  *models.SysDevConfig
	DevFunc    *models.SysDevFunc
	DevSensors []models.SysDevSensor
}

func (d *ProductInsertReq) Generate(model *models.SysDevProduct) error {
	model.Id = utils.SimpleUUID()
	model.ProductModel = strings.TrimSpace(d.ProductModel)
	model.Name = strings.TrimSpace(d.Name)
	if len(model.Name) == 0 {
		model.Name = model.ProductModel
	}
	model.DeviceType = d.DeviceType
	model.Status = false
	model.DeptId = utils.If(len(d.DeptId) > 0, d.DeptId, constant.AnonymousGroupId).(string)
	model.Photo = d.Photo
	model.Description = d.Description
	now := dto.LocalTime(time.Now())
	model.CreateTime = &now
	model.UpdateTime = &now
	model.View = utils.MapToBytes(utils.If(len(d.View) > 0, d.View, map[string]interface{}{}).(map[string]interface{}))
	return nil
}

type ProductEditReq struct {
	Id           string                 `json:"id" vd:"len($)>0"`
	Name         string                 `json:"name" comment:"产品名"`
	DeptId       string                 `json:"deptId" comment:"分组id"`
	Photo        *string                `json:"photo" comment:"产品图片"`
	Description  string                 `json:"description" comment:"产品描述"`
	View         map[string]interface{} `json:"view" comment:"个性页"`
	dto.UserInfo `search:"-"`
}

func (d *ProductEditReq) GenerateMap() map[string]interface{} {
	// 可编辑项
	editMap := map[string]interface{}{}
	if len(d.Name) > 0 {
		editMap["name"] = strings.TrimSpace(d.Name)
	}
	if len(d.DeptId) > 0 {
		editMap["dept_id"] = d.DeptId
	}
	if d.Photo != nil {
		// 可置空
		editMap["photo"] = *d.Photo
	}
	if len(d.Description) > 0 {
		editMap["description"] = d.Description
	}
	if d.View != nil {
		editMap["view"] = utils.MapToBytes(utils.If(len(d.View) > 0, d.View, map[string]interface{}{}).(map[string]interface{}))
	}
	return editMap
}

type ProductDeleteReq struct {
	Id           string `json:"id" vd:"len($)>0"`
	dto.UserInfo `search:"-"`
}

type ProductConfigReq struct {
	Id           string                 `json:"id" vd:"len($)>0"`
	Data         map[string]interface{} `json:"data" comment:"json内容" vd:"len($)>0"`
	dto.UserInfo `search:"-"`
}

type ProductConfigDelReq struct {
	Id  string `json:"id" vd:"len($)>0"`
	Key string `json:"key" vd:"len($)>0"`

	dto.UserInfo `search:"-"`
}

type ProductSensorReq struct {
	Id           string                 `json:"id" vd:"len($)>0"`
	Data         map[string]interface{} `json:"data" comment:"json内容" vd:"len($)>0"`
	dto.UserInfo `search:"-"`

	SenOrSys string
}

func (e *ProductSensorReq) GetSenOrSys() string {
	return e.SenOrSys
}

type ProductSensorDelReq struct {
	Id           string `json:"id" vd:"len($)>0"`
	Key          string `json:"key" comment:"变量key,多个逗号间隔" vd:"len($)>0"`
	dto.UserInfo `search:"-"`

	SenOrSys string
}

func (e *ProductSensorDelReq) GetSenOrSys() string {
	return e.SenOrSys
}

type ProductCopyReq struct {
	Id           string `json:"id" vd:"len($)>0"`
	ProductModel string `json:"productModel"`
	ProductName  string `json:"productName"`
	dto.UserInfo `search:"-"`
}

func (d *ProductCopyReq) GenerateProduct(fromProduct *models.SysDevProduct, productInsert *ProductInsertReq) {
	productInsert.Name = fromProduct.Name + "_copy"
	if len(d.ProductName) > 0 {
		productInsert.Name = d.ProductName
	}
	productInsert.ProductModel = fromProduct.ProductModel + "_" + utils.GetNowTimestampStr()
	if len(d.ProductModel) > 0 {
		productInsert.ProductModel = d.ProductModel
	}
	productInsert.DeviceType = fromProduct.DeviceType
	productInsert.DeptId = fromProduct.DeptId
	productInsert.Photo = fromProduct.Photo
	productInsert.View = utils.BytesToMap(fromProduct.View)
}

type ProductDevSyncReq struct {
	ProductModel string `json:"productModel" vd:"len($)>0"`
	Sn           string `json:"sn"`
	dto.UserInfo `search:"-"`
}

type ProductDeviceConfigReq struct {
	Id           string                 `json:"id" vd:"len($)>0"`
	Data         map[string]interface{} `json:"data" comment:"json内容"`
	Schema       map[string]interface{} `json:"schema" comment:"schema内容"`
	dto.UserInfo `search:"-"`

	NodePath string `json:"nodePath"`
}
